const { BaseServer } = require('../Base')

class HomeServer extends BaseServer {
  // 获取新闻列表
  newsList(ctx) {
    return new Promise((resolve, reject) => {
      let type = ctx.query.type
      this.tools.api
        .apiGet('http://v.juhe.cn/toutiao/index', {
          type: type || 'top',
          key: 'c920749f5196065ad5a8d07ffa90ba4d'
        })
        .then((res) => {
          let resultData = JSON.parse(res)
          if (resultData.error_code === 0) {
            resultData = resultData.result.data
          } else {
            resultData = []
          }
          let navList = [
            { name: '头条', type: 'top' },
            { name: '社会', type: 'shehui' },
            { name: '国内', type: 'guonei' },
            { name: '国际', type: 'guoji' },
            { name: '娱乐', type: 'yule' },
            { name: '体育', type: 'tiyu' },
            { name: '军事', type: 'junshi' },
            { name: '科技', type: 'keji' },
            { name: '财经', type: 'caijing' },
            { name: '时尚', type: 'shishang' }
          ]
          resolve({
            navList: navList,
            list: resultData
          })
        })
        .catch((err) => {
          reject(err)
        })
    })
  }
  // 获取东方新闻网列表
  newsListDf(ctx) {
    let num = ctx.query.num || 3
    return new Promise((resolve, reject) => {
      this.tools.api
        .apiJson('https://apin.eastday.com/api/Special/SpecialNewsList', {
          specialId: 946151,
          skipCount: 0,
          limitCount: num
        })
        .then((res) => {
          resolve(res)
        })
        .catch((err) => {
          reject(err)
        })
    })
  }
}

const ex = new HomeServer()
module.exports = ex
